package com.tanhua.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tanhua.api.UserApi;
import com.tanhua.db.mapper.UserMapper;
import com.tanhua.model.pojo.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByMobile(String mobile) {
        //LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper(User.class);
        //wrapper.eq(User::getMobile, mobile);
        //User user = userMapper.selectOne(wrapper);

        return userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getMobile, mobile));
    }

    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }

    //根据环信id查询用户
    @Override
    public User findByHuanxinId(String huanxinId) {
        return userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getHxUser, huanxinId));
    }

    @Override
    public void updatePhoneById(Long userId, String phone) {
        User user = new User();
        user.setId(userId);
        user.setMobile(phone);
        userMapper.updateById(user);
    }
}
